package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.CanCheckPositionService;
import com.iot08.admin.service.ChannelAreaService;
import com.iot08.admin.service.MenuService;
import com.iot08.admin.service.RoleService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.ChannelArea;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.CanCheckPosition;
import com.iot08.common.response.Response;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Description: 视频规则
 * @author: Rock
 * @time: 2020/8/25 11:20
 */
@Controller
@RequestMapping("position")
public class PositionController extends BaseController {

    @Resource
    private CanCheckPositionService canCheckPositionService;
    @Resource
    private ChannelAreaService channelAreaService;
    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;

    /**
     * 设备位置
     * @param page
     * @time: 2020/10/30 11:41
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page,
                             @RequestParam(value = "searchLocTime", required = false) String searchLocTime) {
        //searchLocTime 如: 2024-09-03
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        Page<CanCheckPosition> pages = canCheckPositionService.list(page, user);
        mv.addObject("positions", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.addObject("menu", menuService.getByName("号码批量分配"));
        mv.addObject("user", user);
        mv.setViewName("position/position_list");
        return mv;
    }

    /**
     * @time: 2020/10/30 17:04
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toDistribution")
    public ModelAndView toDistribution(MultipartFile file) throws Exception {
        ModelAndView mv = new ModelAndView();
        if (file != null && !file.isEmpty()) {
            mv.addObject("positions", canCheckPositionService.distribution(file, this.getSessionUser()));
            mv.addObject("flag", 1);
        } else {
            mv.addObject("flag", 0);
        }
        mv.setViewName("position/position_distribution");
        return mv;
    }

    /**
     * @param id
     * @time: 2020/12/10 14:59
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toOneAllot")
    public ModelAndView toOneAllot(String id) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        if (user.getType() == Const.SYSUSER_TYPE) {
            mv.addObject("msg", "无权限操作");
            mv.setViewName("save_result");
            return mv;
        }
        //获取当前账号区域级别
        int areaLevel = user.getChannelArea() == null ? 1 : user.getChannelArea().getLevel();
        List<ChannelArea> areas = new ArrayList<>();
        if (areaLevel > 1) {
            areas = channelAreaService.listById(user.getChannelAreaId());
        } else {
            areas = channelAreaService.listByLevel(areaLevel - 1, user, true);
        }
        mv.addObject("channelAreas", areas);
        mv.addObject("position", canCheckPositionService.findById(id));
        mv.setViewName("position/device_one_allot");
        return mv;
    }

    /**
     * @param position
     * @time: 2020/12/10 15:33
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "oneAllot", method = RequestMethod.POST)
    public ModelAndView oneAllot(CanCheckPosition position) {
        ModelAndView mv = new ModelAndView();
        canCheckPositionService.oneAllot(position, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @time: 2020/12/10 17:35
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toBatchAllot")
    public ModelAndView toBatchAllot() {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        if (user.getType() == Const.SYSUSER_TYPE) {
            mv.addObject("msg", "无权限操作");
            mv.setViewName("save_result");
            return mv;
        }
        //获取当前账号区域级别
        int areaLevel = user.getChannelArea() == null ? 1 : user.getChannelArea().getLevel();
        List<ChannelArea> areas = new ArrayList<>();
        if (areaLevel > 1) {
            areas = channelAreaService.listById(user.getChannelAreaId());
        } else {
            areas = channelAreaService.listByLevel(areaLevel - 1, user, true);
        }
        mv.addObject("channelAreas", areas);
        mv.setViewName("position/device_batch_allot");
        return mv;
    }

    /**
     * @param channelAreaId
     * @param file
     * @time: 2020/12/10 18:32
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "batchAllot", method = RequestMethod.POST)
    public Response<Object> batchAllot(String channelAreaId, MultipartFile file) throws Exception {
        ModelAndView mv = new ModelAndView();
        if (file == null) {
            return Response.failedResponse("请上传excel文件");
        } else {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            if (!"xls".equals(suffix) && !"xlsx".equals(suffix)) {
                return Response.failedResponse("请上传excel文件");
            }
        }
        return canCheckPositionService.batchAllot(channelAreaId, file, this.getSessionUser(), this.getRemortIP());
    }

}
